; rdboot.asm
; FE3 RAM Disk Basic start code - loading & initialisation

	.include "vicrom.inc"
	.include "fe3def.inc"
	.include "rdglobal.inc"
	.include "rdmem.inc"
	.include "autostart.inc"

	.import rd_main
	
	.import __LRAM_START__
	.import __LRAM_LAST__
	.import __PAGED_START__
	.import __PAGED_LAST__

	BASIC_NEW = $C644
	LOADER = $33C

	.segment "BASIC"

; Startup Basic program "2011 SYS prolog"
			.word	basprg
basprg:		.word	@end
			.word	2012
			.byte	$9E
			.byte	<(prolog / 1000 .mod 10) + $30
			.byte	<(prolog / 100 .mod 10) + $30
			.byte	<(prolog / 10 .mod 10) + $30
			.byte	<(prolog / 1 .mod 10) + $30
			.byte	0
@end:		.word	0

	.segment "BOOT"

sjload_text:	.byte "-> "
sjload_name:	.byte "sjload$04"
	sjload_len = * - sjload_name
			.byte 13, 0

drv_text:	.byte "-> "
drv_name:	.byte "rddrv.bin"
	drv_len = * - drv_name
			.byte 13, 0
main_text:	.byte "-> "
main_name:	.byte "rdmain.bin"
	main_len = * - main_name
			.byte 13, 0
run_name:	.byte "rdrun"
	run_len = * - run_name
			.byte 13, 0

file_err:	lda #<ferr_msg
			ldy #>ferr_msg
			jmp STROUT

prolog:		; check for FE3 SuperRAM mode
			jsr detectFE3
			; print "loading..."
			lda #<load_msg
			ldy #>load_msg
			jsr STROUT
			lda $BA
			bne @do_load
			lda #8
			sta $BA
@do_load:	; try loading sjload
			jsr loadsj
			bcs @nosj
			jsr $0400			
			; load rddrv to lower 3K
@nosj:		lda #<drv_text
			ldy #>drv_text
			jsr STROUT
			lda #drv_len
			ldx #<drv_name
			ldy #>drv_name
			jsr SETNAM
			lda #<__LRAM_START__
			sta ptr
			lda #>__LRAM_START__
			sta ptr+1
			jsr load_bin
			lda #<__LRAM_LAST__
			cmp ptr
			bne file_err
			lda #>__LRAM_LAST__
			cmp ptr+1
			bne file_err
			; - load rdmain to code bank
			lda #<main_text
			ldy #>main_text
			jsr STROUT
			lda #SUPER_RAM_MODE | CODE_BANK
			sta FE3_REG1
			lda #main_len
			ldx #<main_name
			ldy #>main_name
			jsr SETNAM
			lda #<__PAGED_START__
			sta ptr
			lda #>__PAGED_START__
			sta ptr+1
			jsr load_bin
			lda #SUPER_RAM_MODE
			sta FE3_REG1
			lda #<__PAGED_LAST__
			cmp ptr
			bne file_err
			lda #>__PAGED_LAST__
			cmp ptr+1
			bne file_err
			; init RAM Disk
			lda #<init_msg
			ldy #>init_msg
			jsr STROUT
			ldx device_no
			lda #0
			jsr INTOUT
			lda #32
			jsr CHROUT
			lda #'('
			jsr CHROUT
			ldx #<device_no
			lda #>device_no
			jsr INTOUT
			lda #')'
			jsr CHROUT
			lda #13
			jsr CHROUT
			lda #SUPER_RAM_MODE|MAIN_BANK
			sta FE3_REG1
			jsr rd_main
			ldx free_cnt
			lda free_cnt+1
			jsr INTOUT
			lda #<block_msg
			ldy #>block_msg
			jsr STROUT
			jsr init_auto
			jsr cploader
			jmp LOADER

; load bin file to memory @ [ptr]
load_bin:	lda #1			; LFN
			ldx $BA			; device
			ldy #0			; secondary address
			jsr SETLFS
			jsr OPEN
			ldx #1			; set #1 for input
			jsr CHKIN
			bcs @load_fail
@read_loop:	ldy $90			; check status
			bne @eof
			jsr BASIN
			bcs @load_fail
			ldy #0
			sta (ptr), y
			inc ptr
			bne @read_loop
			inc ptr+1
			bne @read_loop
@eof:		jsr CLRCH
			lda #1
			jsr CLOSE
			rts
			
@load_fail:	lda #1
			jsr CLOSE
			pla
			pla
			lda #<fail_msg
			ldy #>fail_msg
			jmp STROUT

; Check for FE3 SuperRAM mode
detectFE3:	; check for regs
			lda FE3_REG1
			cmp #>FE3_REG1
			beq @no_fe3
			lda FE3_REG2
			cmp #>FE3_REG2
			beq @no_fe3
			; Test Super RAM
			ldx #15
@wrt_loop:	txa
			ora #SUPER_RAM_MODE
			sta FE3_REG1
			txa
			sta $2000, x
			sta $4000, x
			sta $6000, x
			sta $A000, x
			dex
			bne @wrt_loop

			ldx #15
@rd_loop:	txa
			ora #SUPER_RAM_MODE
			sta FE3_REG1
			txa
			cmp $2000, x
			bne @no_sram
			cmp $4000, x
			bne @no_sram
			cmp $6000, x
			bne @no_sram
			cmp $A000, x
			bne @no_sram
			dex
			bne @rd_loop
			lda #<fe3ok_msg
			ldy #>fe3ok_msg
			jmp STROUT
			
@no_fe3:	pla
			pla
			lda #<nofe3_msg
			ldy #>nofe3_msg
			jmp STROUT

@no_sram:	pla
			pla
			lda #<nosram_msg
			ldy #>nosram_msg
			jmp STROUT

; load SJLOAD if present
loadsj:		lda #1			; LFN
			ldx $BA			; device
			ldy #0			; secondary address
			jsr SETLFS
			lda #sjload_len
			ldx #<sjload_name
			ldy #>sjload_name
			jsr SETNAM
			jsr OPEN
			ldx #1			; set #1 for input
			jsr CHKIN
			bcs @notfound
			jsr BASIN
			bcs @notfound
			cmp #0
			bne @notfound
			sta ptr
			jsr BASIN
			bcs @notfound
			cmp #$04
			bne @notfound
			sta ptr+1
			lda #<sjload_text
			ldy #>sjload_text
			jsr STROUT
@read_loop:	ldy $90			; check status
			bne @eof
			jsr BASIN
			ldy #0
			sta (ptr), y
			inc ptr
			bne @read_loop
			inc ptr+1
			bne @read_loop
@eof:		jsr CLRCH
			lda #1
			jsr CLOSE
			clc
			rts
@notfound:	jsr CLRCH
			lda #1
			jsr CLOSE
			sec
			rts

nofe3_msg:	.byte 13, "?fe3 not found", 0
nosram_msg: .byte 13, "?fe3 super ram mode", 13, "not available", 0
fe3ok_msg:	.byte 13, "fe3 present!", 13, 0
load_msg:	.byte "loading driver...", 13, 0
fail_msg:	.byte "?load error", 0
init_msg:	.byte "ram disk #", 0
block_msg:	.byte " blocks free", 0
ferr_msg:	.byte "?file size mismatch", 0

;
;	Load & run a basic program (if present)
;		

autorun:	jsr BASIC_NEW
			lda #1			; LFN
			ldx $BA			; device
			ldy #0			; channel
			jsr SETLFS
			lda #run_len
			ldx #<run_name
			ldy #>run_name
			jsr SETNAM
			lda #0			; load
			ldx $2B
			ldy $2C
			jsr LOAD
			bcs loaderr
			stx $2D			; save end address
			sty $2E
			jsr $C659		; CLR
			jsr $C533		; Relinks BASIC Program from and to any address... 
			jmp $C7AE		; BASIC Warm Start (RUN)
loaderr:	jmp $C474

cploader:	ldx #cploader-autorun-1
@cplloop:	lda autorun, x
			sta LOADER, x	; to cassette buffer
			dex
			bpl @cplloop
			rts
